#ifndef _OBJECT_CACHE_H
#define _OBJECT_CACHE_H

namespace NetIO
{

template< class OBJECT, class LOCK = SpinLock >
class ObjectCache
{
public:
	ObjectCache( void )
	{

	};
public:
	~ObjectCache( void )
	{

	}

	static OBJECT * alloc( )
	{
		TRACE_FUNC;
		Guard<LOCK> guard( lock_ );

		if( object_list_.empty( ) )
			return new OBJECT;

		OBJECT *t = object_list_.front( );
		object_list_.pop_front( );
		return t;
	}
	static void free( OBJECT *object )
	{
		TRACE_FUNC;
		Guard<LOCK> guard( lock_ );
		object_list_.push_back( object );
	}

	static void reset( )
	{
		Guard<LOCK> guard( lock_ );

		for( typename std::list<OBJECT*>::iterator iter = object_list_.begin( );
		        iter != object_list_.end( );
		        ++iter )
		{
			delete *iter;
		}

		object_list_.clear( );
	}

private:
	static std::list<OBJECT*> object_list_;
	static LOCK		lock_;
	static int alloc_count_;
	static int free_count_;
};

#ifndef _OBJECT_CACHE_CPP_
#include "ObjectCache.cpp"
#endif

};

#endif

